<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm1({})" v-auth="'asd:ll:asdTmLl:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ djno: record.djno })">
          {{ record.djno }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <InputForm1 @register="registerDrawer1" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsAsdLlAsdTmLlList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { asdTmLlDelete, asdTmLlListData } from '/@/api/asd/ll/asdTmLl';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import InputForm1 from './form1.vue';

  const { t } = useI18n('asd.ll.asdTmLl');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('领料任务管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('发料仓库'),
        field: 'cwhcode',
        component: 'Input',
      },
      {
        label: t('需求仓库'),
        field: 'xqcwhcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'cinvcode',
        component: 'Input',
      },
      {
        label: t('构成比例'),
        field: 'frate',
        component: 'Input',
      },
      {
        label: t('区域'),
        field: 'area',
        component: 'Input',
      },
      {
        label: t('需求时间'),
        field: 'xqDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'ASD_ROW_STATUS',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('单据号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 110,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'ASD_ROW_STATUS',
    },
    {
      title: t('存货编码'),
      dataIndex: 'basInv.cinvcode',
      key: 'inv.cinvcode',
      sorter: true,
      width: 140,
      align: 'left',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'inv.cinvaddcode',
      sorter: true,
      width: 130,
      align: 'left',
      defaultHidden: true,
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('构成比例'),
      dataIndex: 'frate',
      key: 'a.frate',
      sorter: true,
      width: 130,
      align: 'right',
      defaultHidden: true,
    },
    {
      title: t('需求数'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('发料仓库'),
      dataIndex: 'cwhcode',
      key: 'a.cwhcode',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('需求仓库'),
      dataIndex: 'xqcwhcode',
      key: 'a.xqcwhcode',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('选定货位'),
      dataIndex: 'cposcode',
      key: 'a.cposcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('区域'),
      dataIndex: 'area',
      key: 'a.area',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('需求时间'),
      dataIndex: 'xqDate',
      key: 'a.xq_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑领料任务'),
        onClick: handleForm.bind(this, { djno: record.djno }),
        auth: 'asd:ll:asdTmLl:edit',
        ifShow: record.allowDel,
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除领料任务'),
        popConfirm: {
          title: t('是否确认删除领料任务'),
          confirm: handleDelete.bind(this, { djno: record.djno }),
        },
        auth: 'asd:ll:asdTmLl:edit',
        ifShow: record.allowDel,
      },
    ],
  };

  const [registerDrawer, { openDrawer: openDrawer }] = useDrawer();
  const [registerDrawer1, { openDrawer: openDrawer1 }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: asdTmLlListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleForm1(record: Recordable) {
    openDrawer1(true, record);
  }
  async function handleDelete(record: Recordable) {
    const res = await asdTmLlDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
